package com.css.apps.base.user.action;

import com.css.apps.base.common.upload.FileDownload;
import com.css.core.configuration.Environment;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.webbase.webwork.action.UserAction;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.Action;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 
 * @author CSS xinyue
 *
 */
public class DownloadUserTemplate extends UserAction {
	private static final long serialVersionUID = -3246106506374227675L;
	private static Log log = LogFactory.getLog(FileDownload.class);

	public String userGo() {
		InputStream in = null;
		OutputStream out = null;
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.reset();
			response.setHeader("Pragma", "No-cache");
			response.setDateHeader("Expires", 0);
			response.setContentType("application/octet-stream;charset=UTF-8");
			response.setHeader("Content-disposition",
					"attachment;filename=\"" + new String("简历表(模板).docx".getBytes("gb2312"), "ISO8859-1") + "\"");

			in = this.getClass().getResourceAsStream("/userTemplatorWithoutData.docx");
			if (in == null) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.downError"));
				return Action.ERROR;
			}
			out =response.getOutputStream();

			int length = 0;
			byte[] byteBuffer = new byte[1024];
			while ((length=in.read(byteBuffer)) != -1){
				out.write(byteBuffer,0,length);
			}
			out.flush();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			setMessage(Messages.getString("systemMsg.failed"));
			e.printStackTrace();
			return ERROR;
		}finally {
			IOUtils.closeQuietly(out);
			IOUtils.closeQuietly(in);
		}
		return Action.SUCCESS;
	}
	
}
